package helpers;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.servlet.http.HttpServletResponse;

import models.AuthorModel;
import models.AuthorQuestionModel;
import models.QuestionModel;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import systems.RecentlyAuthorUpdate;
import systems.RecentlyUpdate;

import models.CategoryModel;

import com.google.gson.Gson;

import debugs.AuthorDebug;
import debugs.CategoryDebug;
import debugs.QuestionDebug;
import entities.Answer;
import entities.Author;
import entities.AuthorQuestion;
import entities.Category;
import entities.Question;

public class QuestionCrawlerHelper {
	public static Question getQuestion(String qid, HttpServletResponse response) {
		QuestionModel questionModel = QuestionModel.getInstance();
		AuthorQuestionModel aqModel = AuthorQuestionModel.getInstance();
		
		String url = "http://answers.yahoo.com/question/index?qid=" + qid;
		
		Question question = null;
		try {
			String urlServer = "http://" + UrlHelper.fomatDomains(url);
			//Get Question
			Document doc = Jsoup.connect(url).get();
			String qTitle = doc.select("#yan-question .qa-container h1.subject").text();
			String qContent = doc.select("#yan-question .qa-container .content").html();
			String qCreated = doc.select("#yan-question .qa-container ul li:eq(0) abbr").attr("title");
			String alias = StringHelper.replace(qTitle);
			
			question = new Question();
			question.setId(qid);
			question.setTitle(qTitle);
			question.setAlias(alias);
			question.setContent(qContent);
			question.setCreated(DateHelper.StringToDate(qCreated, "yyyy-MM-dd HH:mm:ss Z"));
			question.setUrl(qid);
			
			//Get and set author question
			AuthorModel authorModel = AuthorModel.getInstance();
			Author author = new Author();
			
			String name = doc.select("#yan-question .profile .user .fn").attr("title");
			alias = StringHelper.replace(name);
			String avatar = doc.select("#yan-question .profile .avatar img.photo").attr("src");
			String urlProfile = doc.select("#yan-question .profile .avatar").attr("href");
			urlProfile = URLEncoder.encode(urlServer + urlProfile);
			String uCreated = doc.select("#yan-question .profile dl.stats dd.member").text(); 
			
			author = authorModel.getAuthorByUrl(urlProfile);
			
			if (author == null) {
				author = new Author();
				author.setName(name);
				author.setAlias(alias);
				author.setAvatar(avatar);
				author.setUrl(urlProfile);
				author.setCreated(DateHelper.StringToDate(uCreated, "MMM dd, yyyy"));
				
				author.setId(IdUniqueHelper.getId());
				authorModel.insert(author);
				RecentlyAuthorUpdate.insert(author);
			}
			
			question.setAuthor(author.getId());
			//Update author question
			aqModel.insertQuestion(author.getId(), question.getId());
			
			//Get and set Answer for Question
			Answer answer = new Answer();
			Elements elms = doc.select("#yan-answers .bd ul.shown > li");
			List<String> answers = new ArrayList<String>();
			for (int i = 0; i < elms.size(); i++) {
				
				String aContent = elms.get(i).select(".answer .qa-container .content").html();
				String aCreated = elms.get(i).select(".answer .qa-container ul.meta li:eq(0) abbr").attr("title");
				
				answer = new Answer();
				answer.setId(IdUniqueHelper.getId());
				answer.setContent(aContent);
				answer.setCreated(DateHelper.StringToDate(aCreated, "yyyy-MM-dd HH:mm:ss Z"));
				
				//Get author answer
				name = elms.get(i).select(".answer .profile .user a.url .fn").attr("title");
				alias = StringHelper.replace(name);
				avatar = elms.get(i).select(".answer .profile .avatar img.photo").attr("src");
				urlProfile = urlServer + elms.get(i).select(".answer .profile .avatar").attr("href");
				urlProfile = URLEncoder.encode(urlProfile);
				uCreated = elms.get(i).select(".answer .profile dl.stats dd.member").text();
				
				
				author = authorModel.getAuthorByUrl(urlProfile);
				if (author == null) {
					author = new Author();
					author.setName(name);
					author.setAlias(alias);
					author.setAvatar(avatar);
					author.setUrl(urlProfile);
					author.setCreated(DateHelper.StringToDate(uCreated, "MMM dd, yyyy"));
					author.setId(IdUniqueHelper.getId());
					authorModel.insert(author);
					RecentlyAuthorUpdate.insert(author);
				}
				
				answer.setAuthor(author.getId());
				
				String json = new Gson().toJson(answer);
				answers.add(json);
				
				//Update author answer
				aqModel.insertAnswer(author.getId(), question.getId());
			}
			
			question.setAnswers(answers);
			
			//Get and set category
			Elements cat = doc.select("#yan-categories ul li.current a");
			String catUrl = cat.attr("href");
			String catName = cat.text();
			String catAlias = StringHelper.replace(catName);
			
			CategoryModel categoryModel = CategoryModel.getInstance();
			Category category = categoryModel.getCategoryByAlias(catAlias);

			if (category == null) {
				category = new Category();
				category.setId(IdUniqueHelper.getId());
				category.setUrl(catUrl);
				category.setName(catName);
				category.setAlias(catAlias);
				
				categoryModel.insert(category);
			}
			
			question.setCategory(category.getId());
			
			//Get list question in same category
			Map<String, String> links = new TreeMap<String, String>();
			Elements others = doc.select(".other-questions ul li a");
			for (int i = 0; i < others.size(); i++) {
				String oTitle = others.get(i).text();
				String oUrl = others.get(i).attr("href");
				oUrl = UrlHelper.getIdFromInfoUrl(oUrl);
				links.put(oTitle, oUrl);
				
			}
			
			question.setLinks(new Gson().toJson(links));
			
			//Insert Question 
			questionModel.insert(question);
			//QuestionDebug.debugWeb(question, response);
			
			//Update recently update
			RecentlyUpdate.insert(question);
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return question;
	}
}
